﻿using Entity;
using Entity.ViewModel.UserInfo;
using IDal;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;

namespace Dal
{
    /// <summary>
    /// 用户数据访问层
    /// </summary>
    public class UserInfoDal : BaseDal<UserInfo>, IUserInfoDal
    {

        DecorationDbContext _dbContext;

        public UserInfoDal(DecorationDbContext dbContext) : base(dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public bool CreateUserInfo(UserInfo userInfo)
        {
            _dbContext.UserInfo.Add(userInfo);
            int index = _dbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取所有用户(没有真正查出来的)
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public DbSet<UserInfo> GetUserInfos()
        {
            //using (RepositoryDbContext dbContext = new RepositoryDbContext())
            //{
            //    return dbContext.UserInfos;
            //}


            return _dbContext.UserInfo;

        }

        /// <summary>
        /// 获取所有用户(没有真正查出来的)
        /// </summary>
        public List<UserInfo> GetUserInfos<T>(Expression<Func<UserInfo, bool>> keySelector)
        {
            //using (RepositoryDbContext dbContext = new RepositoryDbContext())
            //{
            //    return dbContext.UserInfos;
            //}


            return _dbContext.UserInfo.Where(keySelector).ToList();

        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        public List<UserInfo> GetUserInfos(Expression<Func<UserInfo, bool>> keySelector)
        {
            //using (RepositoryDbContext dbContext = new RepositoryDbContext())
            //{
            //    return dbContext.UserInfos;
            //}

            //RepositoryDbContext dbContext = new RepositoryDbContext();

            return _dbContext.UserInfo.Where(keySelector).ToList();

        }

        /// <summary>
        /// 获取所有用户(没有真正查出来的)
        /// </summary>
        public DbSet<UserInfo> GetUserInfosDbSet()
        {
            //using (RepositoryDbContext dbContext = new RepositoryDbContext())
            //{
            //    return dbContext.UserInfos;
            //}

            //RepositoryDbContext dbContext = new RepositoryDbContext();

            return _dbContext.UserInfo;

        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateUserInfo(UserInfo entity)
        {
            _dbContext.UserInfo.Update(entity);
            int index = _dbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            return false;
        }
    }
}